# coding:utf-8
import random
from django.core.mail import send_mail

from users.models import UserEmailVerifyCode
from mxonline.settings import EMAIL_FROM

__author__ = 'Miller'
__date__ = '2017/3/21 16:03 '


def make_random_str(length=8):
    str = ''
    chars = 'AaBbCcDdEdFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
    chars_length = len(chars) - 1
    for i in range(length):
        str += chars[random.randint(0, chars_length)]
    return str


def send_register_email(email, send_type='register'):
    email = UserEmailVerifyCode()
    code = make_random_str(16)
    email.code = code
    email.email = email
    email.send_type = send_type
    email.save()
    if send_type == 'register':
        email_title = '暮雪网邮箱激活链接'
        email_body = '暮雪网邮箱激活链接,请点击激活:http://127.0.0.1:8000/active{0}'.format(code)
        send_status = send_mail(email_title, email_body, EMAIL_FROM, [email])
        if send_status:
            print (send_status)
